package com.example.seakdreamandroid;

import static cn.bmob.v3.Bmob.getApplicationContext;

import android.app.Dialog;
import android.app.FragmentManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;

import com.example.seakdreamandroid.entity.User;
import com.example.seakdreamandroid.sleep.AlarmClockAndPrompt;
import com.example.seakdreamandroid.sleep.AlarmItemDetail;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class PayDetailFragment extends DialogFragment {
    private RelativeLayout rePayDetail;
    private LinearLayout linPass;
    private Button btnPayConfirm , btnPay;
    private EditText gridPasswordView;
    private TextView totalPay;
    private ImageView closeDialogFragment;


    private stateListener mStateListener;



    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // 使用不带Theme的构造器, 获得的dialog边框距离屏幕仍有几毫米的缝隙。
        Dialog dialog = new Dialog(getActivity(), R.style.BottomDialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置Content前设定
        dialog.setContentView(R.layout.fragment_pay_detail);
        dialog.setCanceledOnTouchOutside(true); // 外部点击取消


        // 设置宽度为屏宽, 靠近屏幕底部。
        final Window window = dialog.getWindow();
        window.setWindowAnimations(R.style.AnimBottom);
        final WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.BOTTOM; // 紧贴底部
        lp.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度持平
        lp.height = getActivity().getWindowManager().getDefaultDisplay().getHeight() * 3 / 5;
        window.setAttributes(lp);
        rePayDetail = (RelativeLayout) dialog.findViewById(R.id.re_pay_detail);//付款详情
        btnPayConfirm = (Button) dialog.findViewById(R.id.btn_confirm_pay);
        btnPay = dialog.findViewById(R.id.btn_pay);
        gridPasswordView = (EditText) dialog.findViewById(R.id.pass_view);
        linPass = (LinearLayout) dialog.findViewById(R.id.lin_pass);

        totalPay = dialog.findViewById(R.id.total_pay);
        Bundle bundle = getArguments();
        if (bundle != null) {
            totalPay.setText(bundle.getString("Data"));
        }
        btnPayConfirm.setOnClickListener(listener);
        btnPay.setOnClickListener(listener);
        closeDialogFragment = dialog.findViewById(R.id.close_dialogFragment);
        closeDialogFragment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        return dialog;
    }

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Animation slide_left_to_left = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_left_to_left);
            Animation slide_right_to_left = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_right_to_left);
            switch (view.getId()) {
                case R.id.btn_pay:
                    if (gridPasswordView.getText().length()==6){
                        mStateListener.close(totalPay.getText().toString(),"支付成功");
                    }
                    break;
                case R.id.btn_confirm_pay:
                    rePayDetail.startAnimation(slide_left_to_left);
                    rePayDetail.setVisibility(View.GONE);
                    linPass.startAnimation(slide_right_to_left);
                    linPass.setVisibility(View.VISIBLE);
                    break;
                default:
                    break;
            }
        }
    };


    //创建回调，监听dialog的关闭
    public interface stateListener{
        void close(String str,String state);
    }
    public void setStateListener(PayDetailFragment.stateListener listener){
        this.mStateListener = listener;
    }
}
